package org.oos.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.WeakHashMap;

import org.oos.atom.elements.Link;

public class OosImage implements Serializable {

	private static final long serialVersionUID = 7620751014868872554L;

	public static final String REL = "media";

	private String path;
	private String type;

	private static final String DEFAULT_IMAGE_PATH = "http://11870.com/multimedia/logos/xg_logo.png";
	/**
	 * Default Avatar, the location of this avatar is: <br />
	 * <img src="http://11870.com/multimedia/avatar/xg_avatar.png"> <br />
	 * <a href="http://11870.com/multimedia/avatar/xg_avatar.png"
	 * >http://11870.com/multimedia/avatar/xg_avatar.png</a>
	 * 
	 */
	public static final OosImage DEFAULT_IMAGE = new OosImage();

	/**
	 * This enumeration describes the Sized that an image may come in:
	 * 
	 * <table border="1">
	 * <tr>
	 * <td>Small</td>
	 * <td>Medium</td>
	 * <td>Large</td>
	 * <td>Extra Large</td>
	 * </tr>
	 * <tr>
	 * <td>66x66</td>
	 * <td>92x92</td>
	 * <td>320x320</td>
	 * <td>640x640</td>
	 * </tr>
	 * </table>
	 * 
	 * @author pablo.gonzalez@11870.com
	 * 
	 */
	public enum Size {
		SMALL("ps", 66, 66), MEDIUM("pm", 92, 92), LARGE("pl", 320, 320), XLARGE("pxl", 640, 640);

		private String name;
		public int width;
		public int height;
		private Map<OosImage, String> pathCache = new WeakHashMap<OosImage, String>();

		private Size(String smallName, int width, int height) {
			this.name = smallName;
			this.width = width;
			this.height = height;
		}

		/**
		 * Provides with the path for the image for this {@link Size}
		 * 
		 * @param image
		 * @return
		 */
		CharSequence getPath(OosImage image) {
			if (!pathCache.containsKey(image)) {
				String resultUrl;
				if (image.path != null) {
					resultUrl = image.path;
				} else {
					// if there is no link add the default Avatar path
					resultUrl = DEFAULT_IMAGE_PATH;
				}
				// if it is the same size as original
				if (!resultUrl.contains(name)) {
					// check if the original is of other size and
					for (Size size : values()) {
						if (resultUrl.contains(size.name)) {
							resultUrl = resultUrl.replace(size.name, name);
						}
					}
				}
				pathCache.put(image, resultUrl);
			}
			return pathCache.get(image);
		}
	}

	public OosImage() {

	}

	public static OosImage create(String path, String type) {
		OosImage image = new OosImage();
		image.path = path;
		image.type = type;
		return image;
	}

	public static OosImage create(Link link) {
		return create(link.getHref(), link.getType());
	}

	public String getPath(Size size) {
		return size.getPath(this).toString();
	}

	public String getType() {
		return type;
	}

	public static List<OosImage> getImages(List<Link> links) {
		List<OosImage> images = new ArrayList<OosImage>();
		for (Link link : Link.find(links, REL)) {
			images.add(create(link));
		}
		return images;
	}
	
	@Override
	public boolean equals(Object obj) {
		if(obj == null || !(obj instanceof OosImage)) {
			return false;
		} else {
			OosImage other = (OosImage)obj;
			return path.equals(other.path) && type.equals(other.type);
		}
	}

}
